package Utils

import java.text.DecimalFormat

import beans.{AppInfo, Location, Phone, UserBean}
import com.typesafe.config.{Config, ConfigFactory}
import org.apache.commons.lang3.{RandomStringUtils, RandomUtils}

/**
  * Created by hunter.coder 涛哥  
  * 2019/2/11 14:06
  * 交流qq:657270652
  * Version: 1.0
  * 更多学习资料：https://blog.csdn.net/coderblack/
  * Description: 概率工具，用于产生概率控制，以及生成模拟字典数据
  **/
object ProbUtil {

  /**
    * 加载配置参数：数据字典路径
    */
  private val config: Config = ConfigFactory.load()
  private val areafile: String = config.getString("dicts.area")
  private val releasechfile: String = config.getString("dicts.releasech")


  val xy: List[String] = List("1366*768", "1024*768", "1356*768")
  val osnames: List[String] = List("ios", "macos", "android")
  val osvers: List[String] = List("10.0", "9.0", "8.2", "10.05")
  val appvers: List[String] = List("1.0.0", "1.9.0", "1.8.2", "2.0.1", "2.1.9", "2.2.8")
  val prochs: List[String] = List("新浪", "网易", "头条", "抖音", "百度", "腾讯")
  val tmids: List[String] = List("小米科技", "360科技", "OPPO", "VIVO", "华为", "魅族", "中兴", "apple", "锤子", "海尔", "联想")
  val nets: List[String] = List("3G", "4G", "WIFI")
  val carriers: List[String] = List("中国移动", "中国电信", "中国联通", "小米移动", "爱奇艺卡")

  val relechs: List[String] = DictUtil.loadAppRlCh2List(releasechfile)
  val areaList: List[(Int, Option[Double], Option[Double])] = DictUtil.loadArea2List2(areafile)

  /**
    * 控制行为概率
    * @param percent
    * @return
    */
  def probilityDo(percent: Int): Boolean = {
    val rdint = RandomUtils.nextInt(0, 100)
    if (rdint < percent) true else false
  }

  /**
    * 生成随机IMEI码
    * @return
    */
  def imei = {
    RandomStringUtils.random(16, true, true).toUpperCase()
  }

  /**
    * 生成随机屏幕分辨率
    * @return
    */
  def resv: String = {
    val i = RandomUtils.nextInt(0, xy.size)
    xy(i)
  }

  /**
    * 生成版本号
    * @return
    */
  def appv: String = {
    val i = RandomUtils.nextInt(0, appvers.size)
    appvers(i)
  }

  /**
    * 生成手机操作系统名称，安卓id，设备id和品牌id
    * @return
    */
  def os_manu_devid = {
    val i = RandomUtils.nextInt(0, osnames.size)
    val osname = osnames(i)
    var androidid = ""
    var deviceid = ""
    var tmid = "apple"
    if (osname.equals("android")) {
      androidid = RandomStringUtils.random(12, true, true)
      val i = RandomUtils.nextInt(0, tmids.size)
      tmid = tmids(i)
    } else {
      deviceid = RandomStringUtils.random(8, true, true)
    }
    (osname, androidid, deviceid, tmid)
  }

  /**
    * 生成操作系统版本号
    * @return
    */
  def osver: String = {
    val i = RandomUtils.nextInt(0, osvers.size)
    osvers(i)
  }

  /**
    * 生成完整的手机数据实体
    * @return
    */
  def phone: Phone = {
    val im = imei
    val ov = osver
    val rv = resv
    val (osname, andrid, devid, tmid) = os_manu_devid
    new Phone(im, osname, ov, rv, andrid, tmid, devid)
  }

  /**
    * 生成IP地址
    * @return
    */
  def genIp: String = {
    RandomUtils.nextInt(10, 255) + "." + RandomUtils.nextInt(10, 255) + "." + RandomUtils.nextInt(10, 255) + "." + RandomUtils.nextInt(10, 255)
  }

  /**
    *  生成基站cidsn编码
    * @return
    */
  def genCidsn: String = {
    RandomStringUtils.random(12, true, true)
  }

  /**
    * 生成经纬度和区域编码(区域码，经度，维度)
    * @return
    */
  def genLglt: (Int, Option[Double], Option[Double]) = {

    val i = RandomUtils.nextInt(0, areaList.size)
    areaList(i)
  }

  /**
    * 生成位置信息完整实体数据
    * @return
    */
  def location: Location = {

    val area: (Int, Option[Double], Option[Double]) = genLglt

    val ip = RandomUtils.nextInt(10, 255) + "." + RandomUtils.nextInt(10, 255) + "." + RandomUtils.nextInt(10, 255) + "." + RandomUtils.nextInt(10, 255)
    val cid_sn = RandomStringUtils.random(12, true, true)
    val netType = nets(RandomUtils.nextInt(0, nets.size))
    val carrier = carriers(RandomUtils.nextInt(0, carriers.size))
    new Location(area._1,area._2.getOrElse(-1.0),area._3.getOrElse(-1.0), carrier, netType, cid_sn, ip)
  }

  /**
    * 生成app完整实体数据
    * @return
    */
  def appInfo: AppInfo = {
    val av = appvers(RandomUtils.nextInt(0, appvers.size))
    val rch = relechs(RandomUtils.nextInt(0, relechs.size))
    val pch = prochs(RandomUtils.nextInt(0, prochs.size))

    new AppInfo("com.51doit.mall", av, rch, pch)
  }


  /**
    * 生成user完整实体数据
    * @return
    */
  def userBean: UserBean = {
    var uid = ""
    if (ProbUtil.probilityDo(50)) {
      uid = RandomStringUtils.random(9, true, true)
    }
    var account = RandomStringUtils.random(6, true, true)
    var email = RandomStringUtils.random(6, true, true) + "@" + RandomStringUtils.random(5, true, true) + ".com"
    var phoneNbr = RandomStringUtils.randomNumeric(11)
    val df = new DecimalFormat("00")
    var birthday = RandomUtils.nextInt(1960, 2010) + "-" + df.format(RandomUtils.nextInt(1, 13)) + "-" + RandomUtils.nextInt(1, 29)
    var gender = "M"
    if (ProbUtil.probilityDo(50)) gender = "F"
    var addr = RandomStringUtils.random(9, true, false)
    var isregistered = true
    var isLogin = true
    if (uid.isEmpty) {
      isregistered = false
      isLogin = false
      account = ""
      email = ""
      phoneNbr = ""
      birthday = ""
      gender = ""
      addr = ""
    }


    val pho = phone
    val loc = location
    val app = appInfo

    new UserBean(uid, account, email, phoneNbr, birthday, isregistered, isLogin, addr, gender, pho, app, loc)
  }

}


